package com.zk.shop.servlet.Msg;

import com.zk.shop.entity.Goods;
import com.zk.shop.entity.Msg;
import com.zk.shop.entity.User;
import com.zk.shop.service.Impl.MsgServiceImpl;
import com.zk.shop.service.MsgService;
import com.zk.shop.util.JSONServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/AddMsgServlet")
public class AddMsgServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        MsgService msgService = new MsgServiceImpl();
        User user =new User();
        int goodsId =  Integer.parseInt(request.getParameter("goodsId"));
        user.setUserId(1);
        String content = request.getParameter("MsgContent");
       /* String content = "床前明月光";*/
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        Msg msg = new Msg();
        msg.setGoods(goods);
        msg.setConTent(content);
        msg.setUser(user);
        PrintWriter out=response.getWriter();
        if (content!=""){
            int flag =   msgService.addMsg(msg);
            out.print(true);
        }else {
            System.out.println("回复内容为空，不能添加！");
        }


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
